<template>
  <view class="container">
    <float-button name="restaurantDishRecord" />
    <view class="myForm">
      <uni-forms
        ref="valiForm"
        :rules="rules"
        v-model="formData"
        label-position="top"
        label-width="150"
        err-show-type="toast"
      >
        <uni-forms-item label="餐厅" required name="partnerShopId">
          <uni-data-select
            placeholder="请选择餐厅"
            :localdata="treeData"
            v-model="formData.partnerShopId"
          >
          </uni-data-select>
        </uni-forms-item>
        <uni-forms-item label="样品名称" required name="sampleName">
          <uni-easyinput v-model="formData.sampleName" placeholder="请输入" />
        </uni-forms-item>
        <uni-forms-item label="餐别" name="mealType">
          <uni-easyinput v-model="formData.mealType" placeholder="请输入" />
        </uni-forms-item>
        <uni-forms-item label="留样时间" required name="retentionDate">
          <uni-datetime-picker
            type="datetime"
            v-model="formData.retentionDate"
          />
        </uni-forms-item>
        <uni-forms-item label="留样人" required name="retentionPerson">
          <uni-easyinput
            v-model="formData.retentionPerson"
            placeholder="请输入"
          />
        </uni-forms-item>
        <uni-forms-item label="留样重量(g)" required name="retentionWeight">
          <uni-easyinput
            v-model="formData.retentionWeight"
            placeholder="请输入"
          />
        </uni-forms-item>
        <uni-forms-item label="撤样时间" required name="withdrawDate">
          <uni-datetime-picker
            type="datetime"
            v-model="formData.withdrawDate"
          />
        </uni-forms-item>
        <uni-forms-item
          label="留样时长(h)"
          required
          name="retentionDuration"
          :min="0"
        >
          <uni-number-box v-model="formData.retentionDuration" disabled />
        </uni-forms-item>
        <uni-forms-item label="审核人" required name="reviewer">
          <uni-easyinput v-model="formData.reviewer" placeholder="请输入" />
        </uni-forms-item>
        <uni-forms-item label="描述" name="described">
          <uni-easyinput v-model="formData.described" placeholder="请输入" />
        </uni-forms-item>

        <uni-forms-item label="照片" name="oss">
          <FileUploader v-model="formData.oss" :limit="1" />
        </uni-forms-item>
      </uni-forms>
    </view>
    <view class="btnBox">
      <button class="btn" type="primary" @click="onSubmit('valiForm')">
        提交
      </button>
    </view>
  </view>
</template>

<script>
import { getPartnerShop, addRestaurantDishRecord } from '@/api/repast';
import FileUploader from '@/components/FileUploader/index.vue';
export default {
  components: {
    FileUploader,
  },
  data() {
    return {
      treeData: [],
      // 表单数据
      formData: {
        retentionDuration: '',
      },
      // 规则
      rules: {
        partnerShopId: {
          rules: [
            {
              required: true,
              errorMessage: '请选择餐厅',
            },
          ],
        },
        sampleName: {
          rules: [
            {
              required: true,
              errorMessage: '请输入样品名称',
            },
          ],
        },
        retentionDate: {
          rules: [
            {
              required: true,
              errorMessage: '请选择留样时间',
            },
          ],
        },
        retentionPerson: {
          rules: [
            {
              required: true,
              errorMessage: '请输入留样人',
            },
          ],
        },
        retentionWeight: {
          rules: [
            {
              required: true,
              errorMessage: '请输入留样重量(g)',
            },
          ],
        },
        withdrawDate: {
          rules: [
            {
              required: true,
              errorMessage: '请选择撤样时间',
            },
          ],
        },
        retentionDuration: {
          rules: [
            {
              required: true,
              errorMessage: '请输入留样时长(h)',
            },
          ],
        },
        reviewer: {
          rules: [
            {
              required: true,
              errorMessage: '请输入审核人',
            },
          ],
        },
      },
    };
  },
  watch: {
    'formData.retentionDate': {
      handler(newVal) {
        this.computedDays();
      },
      immediate: false,
      deep: true,
    },
    'formData.withdrawDate': {
      handler(newVal) {
        this.computedDays();
      },
      immediate: false,
      deep: true,
    },
  },
  onLoad(query) {
    getPartnerShop().then((res) => {
      this.treeData = res.data.map((item) => {
        return {
          ...item,
          text: item.name,
          value: item.partnerShopId,
        };
      });
    });
  },
  methods: {
    computedDays() {
      if (this.formData.retentionDate && this.formData.withdrawDate) {
        const gap =
          Date.parse(new Date(this.formData.withdrawDate)) -
          Date.parse(new Date(this.formData.retentionDate));
        if (gap > 0) {
          this.formData.retentionDuration = (gap / (1000 * 60 * 60)).toFixed(2);
        } else {
          uni.showToast({
            title: `留样时间不能晚于撤样时间！`,
            icon: 'none',
          });
          this.formData.retentionDuration = 0;
          this.$nextTick(() => {
            this.formData.withdrawDate = null;
          });
        }
      }
    },
    // 提交
    onSubmit(ref) {
      this.$refs[ref]
        .validate()
        .then((res) => {
          console.log('success', res);
          // loading提示
          uni.showLoading({ title: '操作中' });
          // 提交接口
          addRestaurantDishRecord(res).then(() => {
            uni.hideLoading();
            uni.showToast({
              title: `操作成功`,
            });
            this.onReset();
          });
        })
        .catch((err) => {
          console.log('err', err);
        });
    },
    // 重置表单
    onReset() {
      this.formData = this.$options.data().formData;
    },
    // 跳转页面
    onLocation(url) {
      uni.navigateTo({
        url,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  min-height: calc(100vh - var(--window-bottom) - 200rpx);
  padding: 50rpx 36rpx;
  padding-bottom: calc(var(--window-bottom) + 200rpx);
  display: flex;
  flex-direction: column;

  .alert {
    padding: 16rpx;
    margin: 24rpx 0;
    text-align: center;
    background: #ddeafa;
    border-radius: 12rpx;
    text {
      font-size: 32rpx;
      font-weight: 600;
      color: #3a84ff;
    }
  }

  .myForm {
    padding: 24rpx;
    background: #fff;
    border-radius: 12rpx;

    ::v-deep .uni-forms-item__label {
      width: 35%;
      color: #333333;
      font-size: 28rpx;
      font-weight: bold;
      position: relative;

      &::after {
        // content: "";
        width: 100%;
        height: 1rpx;
        background: #000;
        position: absolute;
        left: 14rpx;
        bottom: 20rpx;
      }
    }

    .myLabel {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .text {
        color: #333333;
        font-size: 28rpx;
        font-weight: bold;
      }
      .star {
        color: #dd524d;
      }
    }
  }

  .btnBox {
    position: fixed;
    left: 36rpx;
    right: 36rpx;
    bottom: calc(var(--window-bottom) + 60rpx);
    .btn {
      width: 100%;
      height: 92rpx;
      font-size: 40rpx;
      color: #ffffff;
      line-height: 92rpx;
      background: #3681ff;
    }
  }
}
</style>
